using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Rapier.Kodachi.Shared;

namespace Rapier.Kodachi.WebSearch
{
    public partial class AddEditSearchEngine : Form
    {
        #region Data Members
        private bool addingNew = true;
        private WebSearchEngine engine;
        private WebSearchPlugin webSearchPlugin;
        #endregion

        #region Constructors
        public AddEditSearchEngine(WebSearchPlugin webSearchPlugin)
        {
            InitializeComponent();
            this.webSearchPlugin = webSearchPlugin;
        }

        public AddEditSearchEngine(WebSearchPlugin webSearchPlugin, WebSearchEngine engine) : this(webSearchPlugin)
        {
            addingNew = false;
            Text = "Edit Search Engine";
            this.engine = engine;
        }
        #endregion

        #region Overrides
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (addingNew)
            {
                engine = new WebSearchEngine();
                engine.Name = "New Engine Name";
            }

            tbName.Text = engine.Name;
            tbQueryFormat.Text = engine.QueryFormat.ToLower().Replace("http://", "");
            UpdatePicture();
            
            tbName.Focus();
            if (addingNew)
                tbName.Select(0, tbName.Text.Length);
        }
        #endregion

        #region Private Methods
        private void UpdatePicture()
        {
            pictureBox1.Image = ScaleImage(engine.GetImage(webSearchPlugin));
        }
        #endregion

        #region Event Handlers
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (!tbQueryFormat.Text.Contains("{0}"))
            {
                errorProvider1.SetError(tbQueryFormat, "The query format must contain the string '{0}'\".");
                return;
            }
            
            engine.Name = tbName.Text;
            engine.QueryFormat = "http://" + tbQueryFormat.Text.ToLower().Replace("http://", "");
            if (addingNew)
            {
                if (WebSearchPlugin.WebSearchSettings.Engines.Contains(engine))
                {
                    errorProvider1.SetError(tbName, "The search engine '" + engine.Name + "' already exists. Please enter a different name");
                    return;
                }

                WebSearchPlugin.WebSearchSettings.Engines.Add(engine);
            }

            byte[] imageBytes = webSearchPlugin.Host.DataManager.ConvertObjectToByteArray(pictureBox1.Image);
            webSearchPlugin.Host.DataManager.SaveBinaryData(webSearchPlugin, engine.Name, imageBytes);

            DialogResult = DialogResult.OK;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void linkChangeImage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = false;
            ofd.Filter = "All Image Files|*.bmp;*.png;*.jpg;*.jpeg;*.gif|Bitmap Files|*.bmp|PNG Files|*.png|JPEG Files|*.jpg;*.jpeg|GIF Files|*.gif";
            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            if (ofd.ShowDialog() != DialogResult.OK)
                return;

            Bitmap image = ScaleImage((Bitmap)Bitmap.FromFile(ofd.FileName));

            pictureBox1.Image = image;
            
        }

        private void linkUseDefault_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            webSearchPlugin.Host.DataManager.DeleteBinaryData(webSearchPlugin, engine.Name);
            UpdatePicture();
        }

        private Bitmap ScaleImage(Bitmap image)
        {
            Bitmap newImage;

            double large = Math.Max(image.Width, image.Height);
            double small = Math.Min(image.Width, image.Height);
            double ratio = small / large;
            large = 48;
            small = 48 * ratio;
            if (image.Width > image.Height)
                newImage = (Bitmap)image.GetThumbnailImage((int)large, (int)small, null, IntPtr.Zero);
            else
                newImage = (Bitmap)image.GetThumbnailImage((int)small, (int)large, null, IntPtr.Zero);

            return newImage;
        }
        #endregion
    }
}